from pydantic import BaseModel, Field
from datetime import datetime
from typing import List, Optional


class SearchRequest(BaseModel):
    query_text: str = Field(..., description="查询文本", min_length=1)
    doc_id: Optional[int] = Field(None, description="可选的文档ID，指定后只在该文档中搜索")
    kb_id: Optional[int] = Field(None, description="可选的知识库ID，指定后只在该知识库下所有文档中搜索")
    top_k: int = Field(5, description="返回结果数量", ge=1, le=50)


class SearchResult(BaseModel):
    doc_id: int = Field(..., description="文档ID")
    chunk_index: int = Field(..., description="文本块索引")
    chunk_text: str = Field(..., description="文本块内容")
    similarity: float = Field(..., description="相似度分数", ge=0, le=1)


class SearchResponse(BaseModel):
    success: bool = Field(..., description="请求是否成功")
    query_text: str = Field(..., description="查询文本")
    doc_id: Optional[int] = Field(None, description="搜索的文档ID")
    kb_id: Optional[int] = Field(None, description="搜索的知识库ID")
    results: List[SearchResult] = Field(..., description="搜索结果列表")
    total_results: int = Field(..., description="结果总数")


class RecallHit(BaseModel):
    doc_id: Optional[int] = None
    doc_name: Optional[str] = None
    chunk_index: Optional[int] = None
    chunk_text: str
    score: float


class RecallHistoryItem(BaseModel):
    id: int
    kb_id: int
    query_text: str
    hits: List[RecallHit]
    created_at: datetime


class RecallHistoryListResponse(BaseModel):
    success: bool
    items: List[RecallHistoryItem]


